<?php
/**
 * Created by PhpStorm.
 * User: maqun
 * Date: 2016/2/27
 * Time: 16:56
 */
class ProblemModel extends BaseModel{

    protected $tableName = "problem";//数据库表名

    #导入问题时执行添加问题
    public function addProblem($data){

        $data['in_date']    = date('Y-m-d H:i:s',time());
        $data['defunct']    = 'Y';
        $data['difficulty'] = (int)$data['difficulty'];

        $return = $this->add($data);
        return $return ? $return : 0;
    }
    #判断title为$title的问题是否存在
    public function hasProblem($title){

        $md5   = md5($title);
        $count = $this->where("md5(title)='$md5'")->count();
        $count = intval($count);
        return  ($count>0);

    }
    #通过问题id重判问题
    public function rejudgeProblemByPid($pid){

        $return = array('status'=>'','info'=>'');

        $rjpid=intval($pid);
        $res1 = D('Solution')->where('problem_id='.$rjpid)->save(array('result'=>1));

        $sql = 'select solution_id from solution where problem_id='.$rjpid;
        $res2 = M('Sim')->where("s_id in( $sql )")->delete();

        if(false!==$res1 && $res2){
            $return['status'] = true;
            $return['info']   = "操作成功";
        }else{
            $return['status'] = false;
            $return['info']   = "操作失败,题目可能不存在";
        }
        return $return;

    }
    #通过sulotion_id重判题目
    public function rejudgeProblemBySid($sid){

        $return = array('status'=>'','info'=>'');

        $rjsid=intval($sid);
        $res1 = D('Solution')->where('solution_id='.$rjsid)->save(array('result'=>1));
        $res2 = M('Sim')->where("s_id={$rjsid}")->delete();

        if(false!==$res1 && $res2){
            $return['status'] = true;
            $return['info']   = "操作成功";
        }else{
            $return['status'] = false;
            $return['info']   = "操作失败,题目可能不存在";
        }
        return $return;
    }

}